package com.hruiworks.campusgroup.initializer;

import com.hruiworks.campusgroup.pojo.entity.Activity;
import com.hruiworks.campusgroup.pojo.entity.University;
import com.hruiworks.campusgroup.pojo.query.ActivityQuery;
import com.hruiworks.campusgroup.service.ActivityService;
import com.hruiworks.campusgroup.service.UniversityService;
import com.hruiworks.campusgroup.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.hruiworks.campusgroup.constants.redisKey.NoticeRedisKey.NOTICE_NEW_ACTIVITY_GLOBAL_COUNT;
import static com.hruiworks.campusgroup.constants.redisKey.UniversityRedisKey.UNIVERSITY_ALL;

/**
 * 对redis的预热
 */
@Service
public class RedisPre {

    @Resource
    private UniversityService universityService;

    @Resource
    private ActivityService activityService;

    /**
     * 将学校和对应的key放入redis
     */
    public void universityPre() {
        Long current = 1L;
        Long size = 100L;

        // 所有的活动
        ActivityQuery activityQuery = new ActivityQuery();
        // 只统计过审后的活动
        activityQuery.setActivityStatus(1);
        List<Activity> actList = activityService.listActivity(activityQuery);
        Map<String, Integer> countMap = new HashMap<>();
        Map<String, List<Activity>> map = actList.stream().collect(Collectors.groupingBy(Activity::getUniversity));

        List<University> list = universityService.pageUniversity(size, current);
        while (!list.isEmpty()) {
            RedisUtils.hashSet(UNIVERSITY_ALL, list, University::getName, University::getId);
            for (University university : list) {
                Object schoolKey = RedisUtils.hashGet(UNIVERSITY_ALL, university.getName());
                int count = Objects.isNull(map.get(university.getName())) ? 0 : map.get(university.getName()).size();
                countMap.put(schoolKey.toString(), count);
            }

            list = universityService.pageUniversity(size, ++current);
        }
        RedisUtils.hashSet(NOTICE_NEW_ACTIVITY_GLOBAL_COUNT, countMap);

    }

    /**
     * 统计每个学校的活动数量
     */
    @Deprecated
    public void eachUniversityActivityCount() {
        // 获取所有的活动
        List<Activity> list = activityService.listActivity(new ActivityQuery());
        Map<String, Integer> countMap = new HashMap<>();
        Map<String, List<Activity>> map = list.stream().collect(Collectors.groupingBy(Activity::getUniversity));

        for (Integer key = 1; key<= 2868; key++) {
            Object schoolKey = RedisUtils.hashGet(UNIVERSITY_ALL, key.toString());
            if (Objects.isNull(schoolKey)) {
                continue;
            }
            int count = Objects.isNull(map.get(schoolKey.toString())) ? 0 : map.get(schoolKey.toString()).size();
            countMap.put(schoolKey.toString(), count);
        }
//        RedisUtils.hashDel(NOTICE_NEW_ACTIVITY_GLOBAL_COUNT);
        RedisUtils.hashSet(NOTICE_NEW_ACTIVITY_GLOBAL_COUNT, countMap);
    }

}
